//
//  registerNewAccountController.m
//  QuickCare
//
//  Created by 孙宏毅 on 16/7/12.
//  Copyright © 2016年 lidabiao. All rights reserved.
//

#import "registerNewAccountController.h"
#import <SMS_SDK/SMSSDK.h>
#import "CoreDataManager.h"
#import "UserInfo+CoreDataProperties.h"
#import "SSKeychain.h"
#import "DrawerManager.h"

@interface registerNewAccountController ()
//选择同意协议按钮
@property (weak, nonatomic) IBOutlet UIButton *selectButton;

//下一步按钮
@property (weak, nonatomic) IBOutlet UIButton *nextStepButton;

//输入手机号码会话条
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;

//验证码条
@property (weak, nonatomic) IBOutlet UITextField *authCodeTextField;

//密码条
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

//用户协议按钮
@property (weak, nonatomic) IBOutlet UIButton *accountPotocol;

//获取验证码按钮
@property (weak, nonatomic) IBOutlet UIButton *gainAuthCodeButton;

@property (assign, nonatomic) NSInteger timeCount;

@end

@implementation registerNewAccountController

- (void)viewDidLoad {
    [super viewDidLoad];
  
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [kDrawerManager disableLeftDrawer];
}

//点击选择同意用户协议按钮
- (IBAction)clickSelectButton:(id)sender {
    
    self.selectButton.selected = !self.selectButton.selected;
    
    //同意协议
    if (self.selectButton.selected) {
        //
    }
    else {
        
        
    }
}


//点击下一步按钮
- (IBAction)nextStepButton:(id)sender {
    
    if (!self.selectButton.selected)
    {
        NSLog(@"请先同意协议");
        return;
    }
    
    if (self.phoneNumberTextField.text.length == 0)
    {
        NSLog(@"号码不能为空");
        return;
    }
    if (self.authCodeTextField.text.length == 0)
    {
        NSLog(@"验证码不能为空");
        return;
    }
    if (self.passwordTextField.text.length < 6) {
        NSLog(@"请输入6位以上密码");
        return;
    }
    [SMSSDK commitVerificationCode:self.authCodeTextField.text phoneNumber:self.phoneNumberTextField.text zone:@"86" result:^(NSError *error) {
        
        if (!error)
        {
            NSLog(@"注册成功");
            //储存用户信息到数据库
            UserInfo *userInfo = [NSEntityDescription insertNewObjectForEntityForName:@"UserInfo" inManagedObjectContext:kCoreDataManager.managedObjectContext];
            
            userInfo.phoneNum = self.phoneNumberTextField.text;
            
            [kCoreDataManager save];
            //用户密码存到钥匙串
            NSString *bundleId = [NSBundle mainBundle].bundleIdentifier;
            [SSKeychain setPassword:self.passwordTextField.text forService:bundleId account:self.phoneNumberTextField.text];
        }
        else
        {
            switch (error.code)
            {
                case 456:
                    NSLog(@"手机号码为空");
                    break;
                case 457:
                    NSLog(@"手机号码格式错误");
                    break;
                case 467:
                    NSLog(@"请求校验证码频繁");
                    break;
                case 468:
                    NSLog(@"验证码错误");
                    break;
                default:
                    break;
            }
        }
    }];
}

//点击获取验证码
- (IBAction)gainAuthCode:(UIButton*)sender
{
    if (self.phoneNumberTextField.text.length == 0)
    {
        NSLog(@"号码不能为空");
        return;
    }
    //判断该帐号是否已存在
    NSArray *arr = [kCoreDataManager executeFetchWithEntityName:@"UserInfo" format:[NSString stringWithFormat:@"phoneNum == %@", self.phoneNumberTextField.text]];
    
    if (arr.count > 0)
    {
        NSLog(@"该用户已存在");
        
        return;
    }
    
    if (!self.selectButton.selected) {
        
        NSLog(@"请先同意协议");
        return;
    }
    NSString *phoneNum = self.phoneNumberTextField.text;
    
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:phoneNum zone:@"86" customIdentifier:nil result:^(NSError *error)
    {
        if (!error)
        {
            NSLog(@"获取验证码成功");
            
            self.timeCount = 60;
            
            [sender setTitle:[NSString stringWithFormat:@"%zds", self.timeCount] forState:UIControlStateDisabled];
            
            sender.enabled = NO;
            
            NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
            
            [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
        }
        else
        {
            switch (error.code)
            {
                case 456:
                    NSLog(@"手机号码为空");
                    break;
                case 457:
                    NSLog(@"手机号码格式错误");
                    break;
                case 467:
                    NSLog(@"请求校验证码频繁");
                    break;
                default:
                    break;
            }
        }
    }];
}

-(void)countDown
{
    self.timeCount --;
    
    [self.gainAuthCodeButton setTitle:[NSString stringWithFormat:@"%zds", self.timeCount] forState:UIControlStateDisabled];

    if (self.timeCount == 0)
    {
        self.gainAuthCodeButton.enabled = YES;
        
        [self.gainAuthCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    }
}

//查看用户协议
- (IBAction)clickCheckProtocol:(id)sender {
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
